<template>
  <div>
    <c-form-dialog
      label-width="200px"
      title="项目受理"
      ref="form"
      :form-items="formItems"
      :form-data="formData"
      :table-name="putTableName"
      :before-save="handleBeforeSave"
      :table-id="tableId"
      :auto-close="false"
      :submit="false"
      :dismiss="false"
      @dismiss="handleDismiss"
      :visible.sync="innerVisible"
    >
      <el-form-item>
        <el-button
          @click="handleApprovalPass"
          class="btn-long"
          type="success"
          :loading="approvalLoading"
        >
          确认受理
        </el-button>
        <el-button
          @click="handleApprovalReject"
          class="btn-long"
          type="danger"
          :loading="approvalLoading"
        >
          拒绝受理
        </el-button>
      </el-form-item>
    </c-form-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { phoneValidator } from '@/common/plugins/validator'
export default {
  components: {},
  props: {
    formData: { type: Object, default: () => {} },
    visible: Boolean,
  },
  data() {
    return {
      putTableName: 'customer_related',
      approvalLoading: false,
      formItems: [
        {
          tag: 'input',
          itemAttrs: { label: '申请金额' },
          attrs: { key: 'apply_funds', value: '', disabled: true },
          tagSlots: [{ name: 'append', slot: '万元' }],
        },
        {
          tag: 'number',
          itemAttrs: {
            label: '银行预审金额',
            rules: [
              { required: true, message: '请填写银行预审金额', trigger: 'blur' },
              {
                validator: (rule, value, callback) => {
                  value = Number(value)
                  if (value <= 0) {
                    return callback(new Error('金额不能小于等于0'))
                  }
                  callback()
                },
                trigger: 'blur',
              },
            ],
          },
          attrs: { key: 'bank_approved_funds', value: '' },
          tagSlots: [{ name: 'append', slot: '万元' }],
        },
        {
          tag: 'number',
          itemAttrs: {
            label: '贷款年限',
            rules: [{ required: true, message: '请填写贷款年限', trigger: 'blur' }],
          },
          attrs: { key: 'approval_load_years', value: '' },
          tagSlots: [{ name: 'append', slot: '年' }],
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '项目经理',
            rules: [{ required: true, message: '请填写银行项目经理', trigger: 'blur' }],
          },
          attrs: { key: 'bank_approved_person', class: 'form-control-ml', value: '' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '联系电话',
            rules: [
              { required: true, message: '请输入手机号码', trigger: 'blur' },
              { validator: phoneValidator, message: '请输入正确的手机号码', trigger: 'blur' },
            ],
          },
          attrs: { key: 'bank_approved_person_mobile', value: '', placeholder: '请输入联系电话' },
        },
      ],
    }
  },
  computed: {
    ...mapGetters(['org', 'user', 'clientId']),
    tableId() {
      return this.formData.customer_related_id
    },
    innerVisible: {
      get() {
        return this.visible
      },
      set() {},
    },
  },
  watch: {},
  mounted() {},
  created() {},
  methods: {
    handleDismiss() {
      this.$emit('update:visible', false)
    },
    async handleBeforeSave(model) {
      const id = this.formData.customer_related_id
      this.approvalLoading = true
      const [res] = await this.$utils.promise(
        this.$api.updateData(this.$serviceName, 'customer_related', id, {
          ...model,
          claim_status: 1,
          claim_time: this.$utils.dateFormat(new Date(), 'yyyy-MM-dd hh:mm:ss'),
          bank_id: this.clientId,
          bank_code: this.org.orgCode,
          bank_name: this.org.orgName,
        })
      )
      this.approvalLoading = false
      if (res) {
        this.$message.success('受理成功')
        this.$emit('success', true)
        this.handleDismiss()
      } else {
        this.$message.error('受理失败，请重试')
      }
    },
    handleApprovalPass() {
      this.$refs.form.submit()
    },
    async handleApprovalReject() {
      const id = this.formData.customer_related_id
      this.$prompt('请输入未通过原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputValidator: value => (!value ? '请输入不通过原因' : true),
      })
        .then(({ value }) => {
          this.approvalLoading = true
          this.$api
            .updateData(this.$serviceName, 'customer_related', id, {
              claim_time: this.$utils.dateFormat(new Date(), 'yyyy-MM-dd hh:mm:ss'),
              claim_status: 2,
              bank_id: this.clientId,
              bank_code: this.org.orgCode,
              bank_name: this.org.orgName,
              claim_des: value,
              bank_approved_person: this.user.userId,
            })
            .then(() => {
              this.approvalLoading = false
              this.handleDismiss()
              this.$message.success('项目已拒绝')
              this.$emit('success', true)
            })
            .catch(() => this.$message.error(`项目拒绝失败，请重试`))
        })
        .catch(() => {})
    },
  },
}
</script>
<style lang="scss" scoped></style>
